program ParserTest;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  CPToken in 'CPToken.pas',
  CPBase in 'CPBase.pas',
  CPTokenizer in 'CPTokenizer.pas',
  CPParser in 'CPParser.pas',
  CPOperand in 'CPOperand.pas',
  CPVariables in 'CPVariables.pas',
  CPStringHashMap in 'CPStringHashMap.pas',
  CPBuiltinFunctions in 'CPBuiltinFunctions.pas',
  CPDefaultBuiltinFunctions in 'CPDefaultBuiltinFunctions.pas',
  CPInstruction in 'CPInstruction.pas';

procedure TestTokenizer;
var
  Tokenizer: TCPTokenizer;
  Text: string;
begin
  Tokenizer := TCPTokenizer.Create;
  Text := 'sinwav(x + beta * sinwav(x, omega_m), omega_c)*exp(-x*fade)';
  Tokenizer.Sequence := TCPTextSequence.Create(@Text);
  Tokenizer.Tokenize;
  Tokenizer.Sequence.Free;
  Tokenizer.Free;
end;

procedure TestParser;
var
  Parser: TCPParser;
  Term: TCPParsedTerm;
begin
  Parser := TCPParser.Create;
  Term := Parser.Parse('sin(pi/2)*x+y');
  Parser.Variables['x'].Term := Parser.Parse('20 * y');
  Parser.Variables['y'].Term := Parser.Parse('5');
  Writeln('= ' + FloatToStr(Term.Instructions.Evaluate));
  Writeln(Term.Instructions.ToString);
  Term.Free;
  Parser.Free;
end;

procedure TestStringHashMap;
var
  HashMap: TCPStringHashMap;
  Text: string;
begin
  HashMap := TCPStringHashMap.Create();
  Text := 'This,is,a,test,string';
  HashMap.Values['Test'] := TCPTextSequence.Create(@Text, 1, 5);
  HashMap.Values['bla'] := TCPTextSequence.Create(@Text, 6, 8);
  HashMap.Values['narf'] := TCPTextSequence.Create(@Text, 11, 15);
  Writeln(TCPTextSequence(HashMap.Values['Test']).ToString);
  Writeln(TCPTextSequence(HashMap.Values['bla']).ToString);
  Writeln(TCPTextSequence(HashMap.Values['narf']).ToString);
  HashMap.Free;
end;


procedure Main;
var
  Sequence: TCPTextSequence;
  Text: string;
begin
  Text := 'This is a test';
  Sequence := TCPTextSequence.Create(@Text, 1, Length(Text) + 1);
  Writeln(Sequence.ToString);
  Assert(Length(Sequence.ToString) = Length(Text));
  //Sequence.Start := 0-+-++++--+-1;
  //TestTokenizer;
  //TestStringHashMap;
  TestParser;
end;

begin
  try
    Main;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.
